package tx.yt.charge.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import tx.yt.charge.model.UserInfo;
import tx.yt.charge.service.UserService;
import tx.yt.charge.utils.SessionUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: chenmiao@hupu.com
 * Date: 2015-1-8
 */
@Controller
public class LoginController {

    @Autowired
    UserService userService;

    @Autowired
    DistributionService distributionService;

    @Autowired
    OrderinfoService orderinfoService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String viewLogin() {
        return "login";
    }

  /* @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String toLogin(HttpServletRequest request, HttpServletResponse response) {
        if(SessionUtil.isLogin(request)){
            try {
                response.sendRedirect("/main");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "login";
    }*/

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView login(@RequestParam(value = "username", required = true) String username,
                              @RequestParam(value = "password", required = true) String password,
                              HttpServletRequest request,
                              ModelAndView modelAndView) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            //TODO
        }
        UserInfo userInfo = userService.validateUser(username, password);
        if (null == userInfo) {
            System.out.println("用户名或密码错误");
        } else {
            System.out.println("登陆成功,设置session");
            SessionUtil.setLoginFlag(request, userInfo);
            return new ModelAndView("main");
        }
        return modelAndView;
    }

    @RequestMapping(value = "/loginBs", method = RequestMethod.GET)
    public String viewLoginBs() {
        return "loginBs";
    }

    @RequestMapping(value = "/loginBs", method = RequestMethod.POST)
    public ModelAndView loginBs(@RequestParam(value = "username", required = true) String username,
                                @RequestParam(value = "password", required = true) String password,
                                HttpServletRequest request,
                                ModelAndView modelAndView) {

        /*this.distributionService.queryDistribution(4);*/

        /*Distribution distribution = new Distribution();
        distribution.setId(3);
        this.distributionService.queryDistributions(distribution);*/

        /*Distribution distribution = new Distribution();
        distribution.setDistributionName("杭州");
        this.distributionService.addDistribution(distribution);*/

        /*Distribution distribution = new Distribution();
        distribution.setId(3);
        distribution.setDistributionName("上海分拨点虹口分部");
        this.distributionService.modifyDistribution(distribution);*/

        /*this.distributionService.removeDistribution(7);*/

        /*Orderinfo orderinfo = new Orderinfo();
        orderinfo.setOrderNum("623456789");
        orderinfo.setWeight("40.1");
        orderinfo.setCustomerId("452");
        orderinfo.setDestination("长沙分拨中心");
        orderinfo.setFreight("40");
        orderinfo.setCost(20);
        orderinfo.setBusinessMan("莉莉");
        this.orderinfoService.addOrderinfo(orderinfo);*/

        //this.orderinfoService.queryOrderinfo(1);

        /*Orderinfo orderinfo = new Orderinfo();
        orderinfo.setOrderNum("323456789");
        orderinfo.setWeight("30.1");
        orderinfo.setCustomerId("352");
        orderinfo.setDestination("重庆分拨中心");
        orderinfo.setFreight("30");
        orderinfo.setCost(20);
        orderinfo.setBusinessMan("赵琪");
        this.orderinfoService.queryOrderinfos(orderinfo);*/

       /* Orderinfo orderinfo = new Orderinfo();
        //orderinfo.setOrderNum("323456789");
        this.orderinfoService.queryOrderinfos(orderinfo, 1);*/

        /*Orderinfo orderinfo = new Orderinfo();
        orderinfo.setId(3);
        orderinfo.setOrderNum("323456789");
        orderinfo.setWeight("30.1");
        orderinfo.setCustomerId("352");
        orderinfo.setDestination("重庆分拨中心");
        orderinfo.setFreight("60");
        orderinfo.setCost(30);
        orderinfo.setBusinessMan("赵琪");
        this.orderinfoService.modifyOrderinfo(orderinfo);*/

        /*this.orderinfoService.removeOrderinfo(4);*/

        if(StringUtils.isBlank(username) || StringUtils.isBlank(password)){
            //TODO
        }
        UserInfo userInfo = userService.validateUser(username,password);
        if(null == userInfo){
            System.out.println("用户名或密码错误");
        }
        else{
            System.out.println("登陆成功,设置session");
            SessionUtil.setLoginFlag(request,userInfo);
            return new ModelAndView("mainBs");
        }
        return modelAndView;
    }
}